package com.opengroupe.service.common.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.opengroupe.dao.AccessCodeDao;
import com.opengroupe.dao.CandidatDao;
import com.opengroupe.dao.CandidatReponseQuestionDao;
import com.opengroupe.dao.CategorieDao;
import com.opengroupe.dao.QuestionDao;
import com.opengroupe.dao.ReponseDao;
import com.opengroupe.entities.AccessCode;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.CandidatReponseQuestion;
import com.opengroupe.entities.Categorie;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;
import com.opengroupe.service.common.CrudService;

/**
 * @author MMB11447
 *
 */
public class CrudServiceImpl implements CrudService {
	
	@Autowired
	private AccessCodeDao accessCodeDao;
	
	@Autowired
	private CandidatDao candidatDao;
	
	@Autowired
	private CandidatReponseQuestionDao candidatReponseQuestionDao;
	
	@Autowired
	private CategorieDao categorieDao;
	
	@Autowired
	private QuestionDao questionDao;
	
	@Autowired
	private ReponseDao reponseDao;

	/* (non-Javadoc)
	 * @see com.opengroupe.service.common.CrudService#create(java.lang.Object)
	 */
	@Override
	public Object create(Object toBePersisted) throws Exception {
		if(toBePersisted instanceof AccessCode) {
			return accessCodeDao.persist((AccessCode)toBePersisted);
		} 
		if(toBePersisted instanceof Candidat) {
			return candidatDao.persist((Candidat)toBePersisted);
		}
		if(toBePersisted instanceof CandidatReponseQuestion) {
			return candidatReponseQuestionDao.persist((CandidatReponseQuestion)toBePersisted);
		}
		if(toBePersisted instanceof Categorie) {
			return categorieDao.persist((Categorie)toBePersisted);
		}
		if(toBePersisted instanceof Question) {
			return questionDao.persist((Question)toBePersisted);
		}
		if(toBePersisted instanceof Reponse) {
			return reponseDao.persist((Reponse)toBePersisted);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see com.opengroupe.service.common.CrudService#read(java.lang.Object)
	 */
	@Override
	public Object read(Object objectId, String type) {
		if(type.equals(AccessCode.class.getCanonicalName())) {
			return accessCodeDao.findById((Integer)objectId);
		} 
		if(type.equals(Candidat.class.getCanonicalName())) {
			if(objectId instanceof String) {
				return candidatDao.findByEmail((String)objectId);
			} else {
				return candidatDao.findById((Integer)objectId);
			}
		}
		if(type.equals(CandidatReponseQuestion.class.getCanonicalName())) {
			return candidatReponseQuestionDao.findById((Integer)objectId);
		}
		if(type.equals(Categorie.class.getCanonicalName())) {
			return categorieDao.findByLibelle((String)objectId);
		}
		if(type.equals(Question.class.getCanonicalName())) {
			return questionDao.findById((Integer)objectId);
		}
		if(type.equals(Reponse.class.getCanonicalName())) {
			return reponseDao.findById((Integer)objectId);
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.opengroupe.service.common.CrudService#read(java.lang.String)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List read(String type) {
		if(type.equals(Candidat.class.getCanonicalName())) {
			return candidatDao.findAll();
		} else if(type.equals(Question.class.getCanonicalName())) {
			return questionDao.findAll();
		} else if (type.equals(Reponse.class.getCanonicalName())){
			return reponseDao.findAll();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.opengroupe.service.common.CrudService#update(java.lang.Object)
	 */
	@Override
	public Object update(Object toBeUpdated) throws Exception {
		if(toBeUpdated instanceof AccessCode) {
			return accessCodeDao.merge((AccessCode)toBeUpdated);
		} 
		if(toBeUpdated instanceof Candidat) {
			return candidatDao.merge((Candidat)toBeUpdated);
		}
		if(toBeUpdated instanceof CandidatReponseQuestion) {
			return candidatReponseQuestionDao.merge((CandidatReponseQuestion)toBeUpdated);
		}
		if(toBeUpdated instanceof Categorie) {
			return categorieDao.merge((Categorie)toBeUpdated);
		}
		if(toBeUpdated instanceof Question) {
			return questionDao.merge((Question)toBeUpdated);
		}
		if(toBeUpdated instanceof Reponse) {
			return reponseDao.merge((Reponse)toBeUpdated);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see com.opengroupe.service.common.CrudService#delete(java.lang.Object)
	 */
	@Override
	public void delete(Object toBeDeleted) {
		if(toBeDeleted instanceof AccessCode) {
			accessCodeDao.remove((AccessCode)toBeDeleted);
		} 
		if(toBeDeleted instanceof Candidat) {
			candidatDao.remove((Candidat)toBeDeleted);
		}
		if(toBeDeleted instanceof CandidatReponseQuestion) {
			candidatReponseQuestionDao.remove((CandidatReponseQuestion)toBeDeleted);
		}
		if(toBeDeleted instanceof Categorie) {
			categorieDao.remove((Categorie)toBeDeleted);
		}
		if(toBeDeleted instanceof Question) {
			questionDao.remove((Question)toBeDeleted);
		}
		if(toBeDeleted instanceof Reponse) {
			reponseDao.remove((Reponse)toBeDeleted);
		}
	}

}
